﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;
using System.Configuration;
/*
 缓存助手类
 * 根据缓存策略判断当前项是否需要被缓存
 */
namespace YUE.BA.DataAccess
{
    class CacheHelper
    {

        public static void SetCache<T>(string value, T key)
        {
            var redisClient = CacheHelper.GetClient();
            string hash = value.GetHashCode().ToString();
            redisClient.Set<T>(hash, key);
        }

        #region 创建redis客户端连接对象
        /// <summary>  
        /// 客户端缓存操作对象  
        /// </summary>  
        private static IRedisClient GetClient()
        {
            if (prcm == null)
                CreateManager();

            return prcm.GetClient();
        }


        private static PooledRedisClientManager prcm;
        /// <summary>  
        /// 创建链接池管理对象  
        /// </summary>  
        private static void CreateManager()
        {
            string[] writeServerList = new string[1] { "127.0.0.1:6379" };//SplitString(redisConfigInfo.WriteServerList, ",");
            string[] readServerList = new string[1] { "127.0.0.1:6379" }; //SplitString(redisConfigInfo.ReadServerList, ",");

            prcm = new PooledRedisClientManager(readServerList, writeServerList,
                             new RedisClientManagerConfig
                             {
                                 MaxWritePoolSize = 60, //redisConfigInfo.MaxWritePoolSize,
                                 MaxReadPoolSize = 60, //redisConfigInfo.MaxReadPoolSize,
                                 AutoStart = true //redisConfigInfo.AutoStart,
                             });
        }
        private static string[] SplitString(string strSource, string split)
        {
            return strSource.Split(split.ToArray());
        }
        #endregion
    }
}
